home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #07 (Mar 86) / Pascal 2-3 / Small Flight Source / Small Flight.pas < prev    next >
Pascal/Delphi Source File  |  1986-02-13  |  5KB  |  115 lines

  1.  
  2. program smallflight;                { a scaled-down "star flight" }
  3.  
  4. { Written by Mike Morton for MacTutor }
  5. { Converted to TML Pascal by David E. Smith}
  6.  
  7.     {$I MemTypes.ipas}        {   TML Mac Iibraries }
  8.     {$I QuickDraw.ipas}     {   Quickdraw interface }
  9.     {$I OSIntf.ipas   }     {   Operating System interface }
  10.     {$I ToolIntf.ipas }        {   Toolbox interface }
  11.  
  12.   const
  13.     numStars   = 140;                { number of stars we display }
  14.     maxXY      = 64;                { largest star radius (X or Y) }
  15.     maxZ       = 200;                { largest star distance (Z) }
  16.     speed      = 4;                { Z change per animation cycle }
  17.  
  18.   type
  19.     star = record                { information about one star }
  20.       ploc:    point;                { physical location in space (X, Y) }
  21.       z:       integer;                { physical location in space (Z) }
  22.       sloc:    point;                { location on the screen (h, v) }
  23.     end;
  24.  
  25.   var                        { global program variables }
  26.     stars:     array [0..numStars] of star; { information on stars }
  27.     bounds:    rect;                { rectangle used for bounds checking }
  28.     sorigin:   point;                { center of screen }
  29.     myPort:    grafPort;            { our graphics environment }
  30.     anevent:   eventrecord;            { for checking if the user's bored }
  31.  
  32.   procedure flipPix (h, v: integer); external; { 68000 routine to flip pixel }
  33.  
  34.   { makestar -- randomize physical location; set Z; find screen location. }
  35.   procedure makeStar (VAR new: star);   { initialize one star }
  36.   var dh, dv: integer;                { star's position, relative to origin }
  37.   begin;
  38.     new.ploc.h := random mod maxXY;     { horizontal position }
  39.     new.ploc.v := random mod maxXY;     { vertical position }
  40.     new.z := maxZ;                { how far away is it? }
  41.  
  42.     dh := new.ploc.h*maxZ div new.z;    { compute h offset }
  43.     new.sloc.h := sorigin.h + dh;       { and compute absolute h position }
  44.     dv := new.ploc.v*maxZ div new.z;    { compute v offset }
  45.     new.sloc.v := sorigin.v + dv;       { and compute absolute v position }
  46.  
  47.     flipPix (new.sloc.h, new.sloc.v);   { flip that spot (draw star 1st time) }
  48.   end;                        { of procedure makeStar }
  49.  
  50.   { initialize -- Do Mac initializations; calculate display rect; initialize
  51.     screen; define bounds rect; draw initial stars. }
  52.   procedure initialize;                { one-time initialization }
  53.   var i: integer;                { star number }
  54.   begin;
  55.     initGraf(@thePort);                { fire up quickdraw }
  56.     openPort(@myPort);                { get a drawing environment }
  57.     initCursor;                    { get rid of the Finder's "watch" }
  58.  
  59.     bounds := screenbits.bounds;        { start with the whole screen }
  60.     insetRect (bounds, 25, 30);            { shrink it in a bit }
  61.     sorigin.h := (bounds.left + bounds.right) div 2; { find the... }
  62.     sorigin.v := (bounds.top + bounds.bottom) div 2; { ...origin }
  63.     eraseRect (myPort.portRect);        { clean the screen }
  64.     invertRect (bounds);            { space is black }
  65.     offsetrect (bounds, -sorigin.h, -sorigin.v); { center bounds on origin }
  66.  
  67.     for i := 1 to numStars do            { loop through all the stars.. }
  68.        makeStar (stars [i]);            { ...and make up each one }
  69.   end;                        { procedure init }
  70.  
  71.   { cycle -- main routine.  For each star, erase the old position.  Then see
  72.     if its motion has carried it past the plane we're in.  If so, we create a
  73.     new star.  If not, we compute the new apparent position from the new Z.
  74.     If the apparent position is outside the display, we create a new star;
  75.     otherwise we draw the star's new position. }
  76.   procedure cycle;                { do one animation cycle }
  77.   var
  78.     i:        integer;                { star number in main loop }
  79.     dv, dh: integer;                { star coordinates, origin-relative }
  80.     sp:        ^star;                { fast pointer to stars[i] }
  81.   begin;
  82.     for i := 1 to numStars do            { loop through all the stars }
  83.     begin;
  84.       sp := @stars[i];                { point to star (avoid subscripting) }
  85.       flipPix (sp^.sloc.h, sp^.sloc.v); { erase the star's old position }
  86.  
  87.       sp^.z := sp^.z - speed;            { time advances: find new z position }
  88.       if sp^.z <= 0                { past the plane of the eye yet? }
  89.       then makeStar (sp^)            { yes: this star's gone; make another }
  90.       else begin;                { no: update star's screen position }
  91.         dh := sp^.ploc.h*maxZ div sp^.z; { compute relative h }
  92.         sp^.sloc.h := sorigin.h + dh;   { and compute absolute screen h }
  93.  
  94.         dv := sp^.ploc.v*maxZ div sp^.z; { compute relative v }
  95.         sp^.sloc.v := sorigin.v + dv;   { and compute absolute screen v }
  96.  
  97.         if    (dv >= bounds.bottom)     { is the new position... }
  98.            or (dv <= bounds.top)        { ...outside... }
  99.            or (dh >= bounds.right)      { ...the bounds rectangle... }
  100.            or (dh <= bounds.left)       { ...which is centered at the origin? }
  101.         then makeStar (sp^)            { yes: out of sight, so get a new one }
  102.         else flipPix (sp^.sloc.h, sp^.sloc.v) { no: draw it at new position }
  103.       end;                    { of case where z didn't go off edge }
  104.  
  105.     end;                    { of loop through all stars }
  106.   end;                        { of procedure cycle }
  107.  
  108.   begin;                    { main program }
  109.     initialize;                    { set everything up }
  110.     flushEvents (everyevent, 0);        { ignore stale events }
  111.     repeat                    { main loop: }
  112.       cycle;                    { do one animation "frame" }
  113.     until getnextevent (mDownMask+keyDownMask,anevent); { until click or key }
  114.   end.
  115.